<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Resource factory class
 */

namespace crcl\base;

interface IResource
{
  /**
   * Returns reader container object (singleton)
   *
   * @return \crcl\base\resource\container\IUri $oUri
   **/
  public static function reader($sUri);

//  public static function writer($sAlias = self::DEFAULT_ALIAS);
}

abstract class Resource extends instance\Multiton implements IResource
{
  public static function reader($sUri)
  {
    try
    {
      $oUri = self::getInstance($sUri);
    }
    catch (\crcl\base\instance\Exception $e)
    {
      if ($e->getCode() == $e::ERROR_INVALIDTYPE)
      {
        $sUri = \crcl\base\resource\Utils::replaceScheme('uri', $sUri);
        $oUri = self::getInstance($sUri);
      }
      //@codeCoverageIgnoreStart
      //untestable: just for future use, because right now, no other exception should be thrown
      else
      {
        throw $e;
      }
      //@codeCoverageIgnoreEnd
    }
    return $oUri;
  }

//  public static function writer($sAlias = self::DEFAULT_URI)
//  {
//    $sClass = '\crcl\base\config\Writer';
//    return self::getInstance($sAlias, $sClass, 'writer');
//  }

  /** PROTECTED ***********************************************************************************/

  protected static $sFactoryDirectory = 'resource';
  protected static $sFactoryClass = '\crcl\base\resource\Reader';
}